package library.and.xps.com.step.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import library.and.xps.com.step.EventBusCommond;
import library.and.xps.com.step.R;
import library.and.xps.com.step.m.model.CreateActiveParams;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.view.base.BaseActivity;
import library.and.xps.com.step.view.fragment.TeamChallengeFragment;

public class TeamChallengeActivity extends BaseActivity {
    AutoRelativeLayout ationbar_al;
    ImageView imageView;
    TeamChallengeFragment teamChallengeFragment;

    @Override
    protected int getLayout() {
        return R.layout.activity_team_challenge;
    }

    @Override
    protected void setData() {
        teamChallengeFragment = new TeamChallengeFragment();
        CommonUtils.replaceFragment(R.id.container, teamChallengeFragment, getSupportFragmentManager());
    }

    @Override
    protected void init() {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);

        ((TextView) r2v(R.id.actionbar_tv_title)).setText("团队挑战");
        ationbar_al = r2v(R.id.actionbar_al);
        r2v(R.id.actionbar_iv_back).setOnClickListener(this);
        addPlusButton();
    }

    void addPlusButton() {
        imageView = new ImageView(this);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TeamChallengeActivity.this, CreateChallengeActivity.class));
            }
        });
        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                JUtils.Toast("创建一个挑战");
                return false;
            }
        });

        imageView.setImageResource(R.mipmap.plus);
        AutoRelativeLayout.LayoutParams layoutParams = new AutoRelativeLayout.LayoutParams(AutoRelativeLayout.LayoutParams.WRAP_CONTENT, AutoRelativeLayout.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(AutoRelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.setMargins(40, 40, 25, 40);
        ationbar_al.addView(imageView, layoutParams);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEvent(String  param) {
        if(param.equals(EventBusCommond.FREASH_TEAM_CHALLENGE))
        teamChallengeFragment.onRefresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
        }
    }
}
